package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.dao.WebConfigDao;
import ysq.xyg.web.model.po.WebConfig;
import ysq.xyg.web.model.vo.WebConfigVo;
import ysq.xyg.web.service.WebConfigService;

import java.util.Date;
import java.util.List;

@Service("webConfigService")
public class WebConfigServiceImpl extends BaseServiceImpl<WebConfig> implements WebConfigService {

    @Autowired
    private WebConfigDao webConfigDao;

    @Autowired
    public void setBaseDao(WebConfigDao webConfigDao) {
        this.baseDao = webConfigDao;
    }

    /**
     * 修改
     *
     * @param webConfigVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-18 14:32:48
     */
    @Override
    @CacheEvict(value={"cacheWebConfig"}, allEntries=true)
    public ReturnEntity addOrUpdate(WebConfigVo webConfigVo, SysUser currentUser) {
        boolean isUpate = true;
        WebConfig webConfig;
        if (StrUtil.isEmpty(webConfigVo.getId())) {
            webConfig = new WebConfig(currentUser);
            isUpate = false;
        } else {
            webConfig = selectByPrimaryKey(webConfigVo.getId());
            if (ObjectUtil.isNull(webConfig)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            webConfig.setUpdateTime(new Date());
            webConfig.setUpdateUser(currentUser.getId());
        }
        webConfig.setName(webConfigVo.getName());
        webConfig.setImage(webConfigVo.getImage());
        webConfig.setRecoveryType(webConfigVo.getRecoveryType());
        webConfig.setIndexImage(webConfigVo.getIndexImage());
        webConfig.setPriceMsg(webConfigVo.getPriceMsg());
        webConfig.setAgreeName(webConfigVo.getAgreeName());
        webConfig.setAgreeContent(webConfigVo.getAgreeContent());
        webConfig.setHostManagePhone(webConfigVo.getHostManagePhone());
        int num;
        if (isUpate) {
            num = updateByPrimaryKey(webConfig);
        } else {
            num = insert(webConfig);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    @Override
    public List<WebConfig> acceptList(WebConfigVo webConfigVo) {
        Example example = new Example(WebConfig.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(webConfigVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", webConfigVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(webConfigVo.getAccountId())) {
            criteria.andEqualTo("accountId", webConfigVo.getAccountId());
        }
        example.setOrderByClause(" update_time desc");
        return webConfigDao.selectByExample(example);
    }
}
